Nested ternaries are hard to read and can make the order of operations complex to understand.
function get_readable_status($is_running, $has_errors) {
return $is_running ? "Running" : ($has_errors ? "Failure" : "Succeeded"); // Noncompliant
}
Instead, use another line to express the nested operation in a separate statement.
function get_readable_status($is_running, $has_errors) {
if ($is_running) {
return "Running";
}
return $has_errors ? "Failure. " : "Succeeded ";
}
Exceptions
Exclusively chained shorthand ternary operators ?:
are excluded from this rule.
$result = $option1 ?: $option2 ?: 'default'; // Compliant by exception